{% extends "base.html" %}

{% block title %}Search Archives - Archive Audit System{% endblock %}

{% block content %}
<h2>Search Archives</h2>

<div class="card mb-4">
    <div class="card-header">
        <h5>Search</h5>
    </div>
    <div class="card-body">
        <form method="post">
            <div class="row">
                <div class="col-md-8">
                    <div class="mb-3">
                        <label for="query" class="form-label">Search Query</label>
                        <input type="text" class="form-control" id="query" name="query" required>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="mb-3">
                        <label for="search_type" class="form-label">Search In</label>
                        <select class="form-select" id="search_type" name="search_type">
                            <option value="all">All Archives</option>
                            <option value="db">Database Archives</option>
                            <option value="file">File Archives</option>
                            <option value="content">File Contents</option>
                        </select>
                    </div>
                </div>
            </div>
            <button type="submit" class="btn btn-primary">Search</button>
        </form>
    </div>
</div>

{% if db_results %}
<div class="card mb-4">
    <div class="card-header">
        <h5>Database Archives</h5>
    </div>
    <div class="card-body">
        <div class="table-responsive">
            <table class="table table-striped table-hover">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>Original Database</th>
                        <th>Archive Date</th>
                        <th>Description</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    {% for archive in db_results %}
                    <tr>
                        <td>{{ archive.id }}</td>
                        <td>{{ archive.original_db }}</td>
                        <td>{{ archive.archive_date }}</td>
                        <td>{{ archive.description }}</td>
                        <td>
                            <a href="{{ url_for('audit_db', archive_id=archive.id) }}" class="btn btn-sm btn-primary">Audit</a>
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
</div>
{% endif %}

{% if file_results %}
<div class="card mb-4">
    <div class="card-header">
        <h5>File Archives</h5>
    </div>
    <div class="card-body">
        <div class="table-responsive">
            <table class="table table-striped table-hover">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>Original Path</th>
                        <th>Archive Date</th>
                        <th>Description</th>
                        <th>File Count</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    {% for archive in file_results %}
                    <tr>
                        <td>{{ archive.id }}</td>
                        <td>{{ archive.original_path }}</td>
                        <td>{{ archive.archive_date }}</td>
                        <td>{{ archive.description }}</td>
                        <td>{{ archive.file_count }}</td>
                        <td>
                            <a href="{{ url_for('audit_files', archive_id=archive.id) }}" class="btn btn-sm btn-primary">Browse</a>
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
</div>
{% endif %}

{% if file_content_results %}
<div class="card mb-4">
    <div class="card-header">
        <h5>Files</h5>
    </div>
    <div class="card-body">
        <div class="table-responsive">
            <table class="table table-striped table-hover">
                <thead>
                    <tr>
                        <th>Archive ID</th>
                        <th>Path</th>
                        <th>Type</th>
                        <th>Size</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    {% for file in file_content_results %}
                    <tr>
                        <td>{{ file.archive_id }}</td>
                        <td>{{ file.relative_path }}</td>
                        <td>{{ file.file_type or 'Unknown' }}</td>
                        <td>{{ (file.file_size / 1024)|round(1) }} KB</td>
                        <td>
                            <a href="{{ url_for('audit_files', archive_id=file.archive_id) }}" class="btn btn-sm btn-primary">View Archive</a>
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
</div>
{% endif %}

{% if request.method == 'POST' and not db_results and not file_results and not file_content_results %}
<div class="alert alert-info">
    No results found for your search query.
</div>
{% endif %}
{% endblock %}
